import 'package:flutter/material.dart';
import 'package:flutterroom/pages/home/widgets/common_image.dart';

class SearcgBar extends StatefulWidget {
  final bool showLoaction; //展示位置按钮
  final Function goBackCallback; //回退按钮函数
  final String inputValue; // 搜索框输入值
  final String defaultInputValue; // 搜索框默认值
  final Function onCancel; //取消按钮
  final bool showMap; //展示地图按钮
  final Function onSearch; //用户点击搜索框触发
  final ValueChanged<String> onSearchSubmit;

  const SearcgBar(
      {Key key,
      this.showLoaction,
      this.goBackCallback,
      this.inputValue = "",
      this.defaultInputValue = "请输入搜索词",
      this.onCancel,
      this.showMap,
      this.onSearch,
      this.onSearchSubmit})
      : super(key: key);

  @override
  _SearcgBarState createState() => _SearcgBarState();
}

class _SearcgBarState extends State<SearcgBar> {
  String _searchWord = "";
  TextEditingController _controller;
  //焦点
  FocusNode _focus;
  Function _onClean() {
    _controller.clear();
    setState(() {
      _searchWord = "";
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    _controller = TextEditingController(text: widget.inputValue);
    _focus = FocusNode();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Row(
        children: <Widget>[
          if (widget.showLoaction != null)
            GestureDetector(
              onTap: () {},
              child: Row(
                children: <Widget>[
                  Icon(
                    Icons.room,
                    color: Colors.green,
                    size: 16,
                  ),
                  Text(
                    "北京",
                    style: TextStyle(color: Colors.black, fontSize: 14),
                  )
                ],
              ),
            ),
          if (widget.showLoaction != null) Padding(padding: EdgeInsets.all(10)),
          if (widget.goBackCallback != null)
            GestureDetector(
              onTap: () {},
              child: Icon(
                Icons.chevron_left,
                color: Colors.black26,
              ),
            ),
          if (widget.goBackCallback != null)
            Padding(padding: EdgeInsets.all(10)),
          Expanded(
              child: Container(
                  height: 34,
                  decoration: BoxDecoration(
                      color: Colors.grey[200],
                      borderRadius: BorderRadius.circular(17)),
                  // padding: EdgeInsets.only(right: 10),
                  child: TextField(
                    focusNode: _focus,
                    controller: _controller,
                    onTap: () {
                      if (widget.onSearchSubmit == null) {
                        _focus.unfocus();
                      }
                      widget.onSearch();
                    },
                    onSubmitted: widget.onSearchSubmit,
                    textInputAction: TextInputAction.search,
                    onChanged: (String value) {
                      setState(() {
                        _searchWord = value;
                      });
                    },
                    style: TextStyle(fontSize: 14, color: Colors.black),
                    decoration: InputDecoration(
                        contentPadding: EdgeInsets.only(top: 1, left: -10),
                        hintText: "请输入关键字搜索",
                        border: InputBorder.none,
                        suffixIcon: GestureDetector(
                          onTap: () {
                            _onClean();
                          },
                          child: Icon(
                            Icons.clear,
                            size: 18,
                            color: _searchWord == ""
                                ? Colors.grey[200]
                                : Colors.grey,
                          ),
                        ),
                        icon: Padding(
                          padding: const EdgeInsets.only(top: 4.0, left: 8),
                          child: Icon(
                            Icons.search,
                            size: 18,
                            color: Colors.grey,
                          ),
                        )),
                  ))),
          if (widget.onCancel != null) Padding(padding: EdgeInsets.all(10)),
          if (widget.onCancel != null)
            GestureDetector(
              onTap: () {},
              child: Text(
                "取消",
                style: TextStyle(color: Colors.black, fontSize: 15),
              ),
            ),
          if (widget.showMap != null)
            CommonImage("static/images/widget_search_bar_map.png")
        ],
      ),
    );
  }
}
